package com.ai.deepseekspring.utils;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class RequestUtil {

    /**
     * 获取当前请求的HttpServletRequest对象
     */
    public static HttpServletRequest getCurrentRequest() {
        // 从请求上下文获取当前请求
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        if (attributes == null) {
            throw new RuntimeException("当前环境不存在请求上下文");
        }
        return attributes.getRequest();
    }

    /**
     * 从当前请求头中获取Token
     */
    public static String getTokenFromHeader() {
        HttpServletRequest request = getCurrentRequest();


        String authorizationHeader = request.getHeader("token");
        System.out.println(authorizationHeader);


        if (authorizationHeader == null) {
            return null; // 或抛出异常
        }
        return authorizationHeader;
    }
}